package org.fuys.etp.core.reject.impl;

import org.fuys.etp.core.model.event.EtpAlertEvent;
import org.fuys.etp.core.model.vo.TpConfigVO;
import org.fuys.etp.core.pool.EtpThreadPoolExecutor;
import org.fuys.etp.core.reject.EtpRejectHandler;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @projectName: fuys-etp-final
 * @package: org.fuys.etp.core.reject.impl
 * @className: AlertRejectHandler
 * @author: WangJing
 * @description: 警告型策略
 * @date: 2025/2/18 22:15
 * @version: 1.0
 */
@Component
@Scope("prototype")
public class AlertRejectHandler extends EtpRejectHandler {

    @Resource
    private ApplicationEventPublisher eventPublisher;

    @Override
    protected void doRejectHandle(Runnable r, ThreadPoolExecutor executor) {
        if (executor instanceof EtpThreadPoolExecutor) {
            EtpThreadPoolExecutor etp = (EtpThreadPoolExecutor) executor;
            final TpConfigVO actualConfig = etp.getActualConfig();
            final long count = super.rejectCount();
            if (count > actualConfig.getBaseConfig().getRejectAlertThreshold()) {
                EtpAlertEvent alertEvent = new EtpAlertEvent(this, etp.statusExplore(),
                        etp.getAlertType());
                super.removeCount();
                eventPublisher.publishEvent(alertEvent);
            }
        }
    }
}
